package giro.general;

import giro.general.ranking.RankingFinderActivity;
import giro.general.rss.RssActivity;
import giro.general.rss.RssActivityOld;
import giro.general.stage.StagesActivity;
import giro.general.stage.finder.StagesFinderActivity;
import giro.general.team.Squadre;
import giro.general.util.Commons;
import giro.general.util.Parser;
import giro.general.video.VideoActivity;

import java.util.Locale;

import org.json.JSONArray;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import appReate.Net.ConnectNet;

public class GiroItaliaActivity extends BaseActivity {
	private JSONArray mStagesArray;
	private JSONArray mTappeArray;
	private boolean mBackAlreadyPressed;
	private Activity mActivity;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		checkFirstLogin();

	}

	private void checkFirstLogin() {

		int count = 0;

		SharedPreferences settings = getSharedPreferences("giro", Activity.MODE_PRIVATE);
		if (settings != null) {
			count = settings.getInt("numLogin", 0);
			if (count % 5 == 1) {
				new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle(R.string.warning)
						.setMessage(R.string.msg_warning)
						.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog, int which) {
								startGiro();
							}

						}).show();

			} else {
				startGiro();
			}
			SharedPreferences.Editor editor = settings.edit();

			count++;
			editor.putInt("numLogin", count);
			editor.commit();

		}

	}

	public void startGiro() {
		final Button info_tappe = (Button) findViewById(R.id.info_tappe);
		final Button filtro_tappe = (Button) findViewById(R.id.filtro_tappe);
		final Button classifica = (Button) findViewById(R.id.classifiche);
		final Button squadre = (Button) findViewById(R.id.squadre);
		final Button rss = (Button) findViewById(R.id.rss);
		final Button video = (Button) findViewById(R.id.video);

		mStagesArray = Parser.getInstance().createArrayJson(this, R.raw.tappe);
		mTappeArray = Parser.getInstance().createTappeArrayJson(mStagesArray);
		mActivity = this;

		Commons.getInstance().setStagesArray(mStagesArray);
		Commons.getInstance().setTappeArray(mTappeArray);
		Commons.getInstance().setTappeArrayJsonBackup(mTappeArray);

		// attiva bottone classifica
		classifica.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				Intent intent = new Intent(GiroItaliaActivity.this, RankingFinderActivity.class);
				startActivity(intent);
				// Intent intent = new Intent(GiroItaliaActivity.this,
				// WebViewGiro.class);
				// intent.putExtra(
				// "url",
				// "http://www.gazzetta.it/Speciali/Giroditalia/2012/classifiche/it/t_01_class.shtml?lang=it");
				// startActivity(intent);
			}
		});

		filtro_tappe.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				Intent intent = new Intent(GiroItaliaActivity.this, StagesFinderActivity.class);
				startActivity(intent);

			}
		});

		info_tappe.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				Intent intent = new Intent(GiroItaliaActivity.this, StagesActivity.class);
				startActivity(intent);

			}
		});

		rss.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				// Intent intent = new Intent(GiroItaliaActivity.this,
				// RssActivity.class);
				// startActivity(intent);

				if (ConnectNet.isOnline(mActivity)) {

					Intent intent;
					if (Locale.getDefault().equals(Locale.ITALIAN) || Locale.getDefault().equals(Locale.ITALY)) {
						intent = new Intent(GiroItaliaActivity.this, RssActivity.class);
					} else {
						intent = new Intent(GiroItaliaActivity.this, RssActivityOld.class);
					}
					startActivity(intent);
				} else {
					Toast.makeText(mActivity, "No Internet connected", Toast.LENGTH_LONG).show();
				}
			}
		});

		// attiva bottone squadre

		squadre.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				Intent intent = new Intent(GiroItaliaActivity.this, Squadre.class);
				startActivity(intent);

			}
		});
		// attiva bottone video

		video.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				if (ConnectNet.isOnline(mActivity)) {

					Intent intent = new Intent(GiroItaliaActivity.this, VideoActivity.class);
					startActivity(intent);
				} else {

					Toast.makeText(mActivity, "No Internet connected", Toast.LENGTH_LONG).show();

				}
			}
		});

	}

	@Override
	public void onBackPressed() {
		if (mBackAlreadyPressed) {
			finish();
		} else {
			mBackAlreadyPressed = true;
			Toast.makeText(this, R.string.press_back_again, Toast.LENGTH_SHORT).show();
		}

	}

	@Override
	protected void onPause() {
		mBackAlreadyPressed = false;
		super.onPause();
	}

}